package com.feeyo.groundservice.recordnfc.activity.view

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ScreenUtils
import com.feeyo.groundservice.base_util.CallbackWithData
import com.feeyo.groundservice.base_util.FyLog
import com.feeyo.groundservice.base_util.SuccessCallback
import com.feeyo.groundservice.base_util.getConfirmDlg
import com.feeyo.groundservice.base_util.getPxFromResource
import com.feeyo.groundservice.base_util.toJson
import com.feeyo.groundservice.base_util.toastCenter
import com.feeyo.groundservice.composewidget.util.dimenToDp
import com.feeyo.groundservice.composewidget.util.dimenToSp
import com.feeyo.groundservice.composewidget.util.intervalClick
import com.feeyo.groundservice.composewidget.util.pxToDp
import com.feeyo.groundservice.composewidget.util.toComposeColor
import com.feeyo.groundservice.composewidget.view.button.CustomButton
import com.feeyo.groundservice.composewidget.view.menu_fab.FloatDraggableButton
import com.feeyo.groundservice.composewidget.view.tab.SwitchTab
import com.feeyo.groundservice.composewidget.view.title_bar.CommonTitleView
import com.feeyo.groundservice.composewidget.view.util.HorizontalSpace
import com.feeyo.groundservice.composewidget.view.util.VerticalSpace
import com.feeyo.groundservice.recordnfc.R
import com.feeyo.groundservice.recordnfc.constant.AttachResourceType
import com.feeyo.groundservice.recordnfc.constant.RecorderType
import com.feeyo.groundservice.recordnfc.constant.ResourceType
import com.feeyo.groundservice.recordnfc.constant.currentSite
import com.feeyo.groundservice.recordnfc.constant.getTabMenus
import com.feeyo.groundservice.recordnfc.constant.statusBarColor
import com.feeyo.groundservice.recordnfc.dlg.NfcLogPopView
import com.feeyo.groundservice.recordnfc.dlg.ResourceListPopView
import com.feeyo.groundservice.recordnfc.model.GateItemBO
import com.feeyo.groundservice.recordnfc.model.MixNfcTypeBO
import com.feeyo.groundservice.recordnfc.model.NfcCarBO
import com.feeyo.groundservice.recordnfc.model.NfcUserBO
import com.feeyo.groundservice.recordnfc.model.ParkingItemBO
import com.feeyo.groundservice.recordnfc.model.TaskItemBO
import com.feeyo.groundservice.recordnfc.viewmodel.RecorderCardViewModel
import com.lxj.xpopup.XPopup

/**
 *作者：70893 on 2025-10-08
 *描述：
 */


@Composable
internal fun MainView(
    nfcId: MutableState<String>, iatas: List<String>
) {
    val defaultIndex = 0
    val mCurrentSelectTab = remember { mutableIntStateOf(defaultIndex) }//当前选中的tab
    val viewModel: RecorderCardViewModel = viewModel()

    //默认位置是机位类型
    val positionType = remember { mutableStateOf(AttachResourceType.PARKING) }

    //资源
    val carNo = remember { mutableStateOf<NfcCarBO?>(null) }
    val person = remember { mutableStateOf<NfcUserBO?>(null) }

    val tasks = remember { mutableStateListOf<TaskItemBO>() }

    val parking = remember { mutableStateOf<ParkingItemBO?>(null) }
    val gate = remember { mutableStateOf<GateItemBO?>(null) }


    Box(
        modifier = Modifier.fillMaxSize()
    ) {
        Column {
            CommonTitleView(
                title = "NFC录卡",
                bg = statusBarColor.toComposeColor(),
                topPadding = BarUtils.getStatusBarHeight(),
                onBack = {
                    ActivityUtils.getTopActivity()?.finish()
                },
                rightContent = {
                    RightMenuView(iatas)
                })

            //tab
            Row(
                modifier = Modifier.background(color = R.color.white.toComposeColor())
                    .padding(vertical = R.dimen.d4.dimenToDp()),
            ) {
                HorizontalSpace(R.dimen.d10.dimenToDp())
                Spacer(modifier = Modifier.weight(1f))//占位的
                SwitchTab(
                    tabRowItems = getTabMenus(),
                    onSelectIndex = {
                        mCurrentSelectTab.intValue = it
                    },
                    defaultIndex = defaultIndex,
                    bottomHeight = R.dimen.d48.dimenToDp(),
                    bottomBgColor = R.color.white.toComposeColor(),
                    bottomCornerRadius = 0.dp,
                    selectedTextColor = statusBarColor.toComposeColor(),
                    unSelectedTextColor = R.color.black_27.toComposeColor().copy(alpha = 0.55f),
                    selectedTextSize = R.dimen.d16.dimenToDp(),
                    unSelectedTextSize = R.dimen.d15.dimenToDp(),
                    padding = 0.dp,
                    slideBarGapWidth = R.dimen.d10.dimenToDp(),
                    slideBarWidth = (ScreenUtils.getScreenWidth()
                        .pxToDp() - R.dimen.d20.dimenToDp()) / getTabMenus().size,
                    slideBarBgColor = statusBarColor.toComposeColor().copy(alpha = 0.3f),
                    slideBarCornerRadius = R.dimen.d6.dimenToDp(),
                )
                Spacer(modifier = Modifier.weight(1f))
                HorizontalSpace(R.dimen.d10.dimenToDp())
            }

            when (mCurrentSelectTab.intValue) {
                RecorderType.PERSON.index -> // 人员
                    PersonEntry(person = person, nfcId = nfcId)

                RecorderType.CAR.index -> //车辆
                    CarEntry(carNo = carNo, nfcId = nfcId)

                RecorderType.TASK.index -> //任务
                    TaskEntry(
                        tasks = tasks, parking = parking, gate = gate, nfcId = nfcId,
                        onSelectType = {
                            positionType.value = it
                        }
                    )

                else -> {}
            }

            Box(
                modifier = Modifier.fillMaxWidth().padding(horizontal = R.dimen.d15.dimenToDp())
            ) {
                CustomButton(
                    modifier = Modifier.fillMaxWidth().height(R.dimen.d36.dimenToDp()),
                    bgs = listOf(
                        statusBarColor.toComposeColor(), statusBarColor.toComposeColor().copy(0.8f)
                    ),
                    cornerShape = RoundedCornerShape(R.dimen.d5.dimenToDp()),
                    onClick = {
                        when (mCurrentSelectTab.intValue) {
                            RecorderType.PERSON.index -> {
                                //参数为：人 nfc
                                assert(person.value != null) {
                                    toastCenter("请选择用户")
                                    return@CustomButton
                                }
                                assert(nfcId.value.isNotEmpty()) {
                                    toastCenter("请输入NFC")
                                    return@CustomButton
                                }
                                //先根据nfc查询
                                viewModel.queryNfcBindType(
                                    nfcId.value, object : CallbackWithData<MixNfcTypeBO> {
                                        override fun success(data: MixNfcTypeBO) {
                                            super.success(data)
                                            var msg: String? = null
                                            data.user?.firstOrNull()?.let {
                                                //已绑定：自己或者别人
                                                if (it.uid == person.value?.uid) {
                                                    toastCenter("该NFC已绑定此用户了")
                                                } else {
                                                    msg =
                                                        "此NFC卡已经绑定到${it.truename},是否需要强制绑定到${person.value?.truename}?"
                                                }
                                            } ?: data.car?.firstOrNull()?.let {
                                                //nfc绑定到车上，需要换绑到人上
                                                msg =
                                                    "此NFC卡已经绑定到${it.title},是否需要强制绑定到${person.value?.truename}?"

                                            } ?: data.parking?.firstOrNull()?.let {
                                                msg =
                                                    "此NFC卡已经绑定到${it.title},是否需要强制绑定到${person.value?.truename}?"

                                            } ?: run {
                                                //新的NFC卡,直接绑定
                                                msg = ""
                                            }

                                            msg?.let {
                                                forceUpdateUerInfo(
                                                    viewModel = viewModel,
                                                    uid = person.value!!.uid,
                                                    nfcId = nfcId.value,
                                                    msg = it,
                                                    isForce = it.isNotEmpty(),
                                                    onSuccess = {
                                                        person.value = null
                                                        nfcId.value = ""
                                                    }
                                                )
                                            }
                                        }
                                    })
                            }

                            RecorderType.CAR.index -> {
                                //参数为：车 nfc
                                assert(carNo.value != null) {
                                    toastCenter("请选择车辆")
                                    return@CustomButton
                                }
                                assert(nfcId.value.isNotEmpty()) {
                                    toastCenter("请输入NFC")
                                    return@CustomButton
                                }
                                //先根据nfc查询
                                viewModel.queryNfcBindType(
                                    nfcId.value, object : CallbackWithData<MixNfcTypeBO> {
                                        override fun success(data: MixNfcTypeBO) {
                                            super.success(data)
                                            var msg: String? = null

                                            data.user?.firstOrNull()?.let {
                                                //nfc绑定到人上，需要换绑到车上
                                                msg =
                                                    "此NFC卡已经绑定到${it.truename},是否需要强制绑定到${carNo.value?.title}?"

                                            } ?: data.car?.firstOrNull()?.let {
                                                //已绑定：自己或者其他车
                                                if (it.id == carNo.value!!.id) {
                                                    toastCenter("此NFC已绑定到当前车辆")
                                                } else {
                                                    msg =
                                                        "此NFC卡已经绑定到${it.title},是否需要强制绑定到${carNo.value?.title}?"
                                                }
                                            } ?: data.parking?.firstOrNull()?.let {
                                                msg =
                                                    "此NFC卡已经绑定到${it.title},是否需要强制绑定到${carNo.value?.title}?"

                                            } ?: run {
                                                //新的NFC卡,直接绑定
                                                msg = ""
                                            }

                                            msg?.let {
                                                forceUpdateCarInfo(
                                                    viewModel = viewModel,
                                                    carId = carNo.value!!.id,
                                                    nfcId = nfcId.value,
                                                    msg = it,
                                                    isForce = it.isNotEmpty(),
                                                    onSuccess = {
                                                        carNo.value = null
                                                        nfcId.value = ""
                                                    }
                                                )
                                            }
                                        }
                                    })
                            }

                            RecorderType.TASK.index -> {
                                //参数为：任务 nfc   机位、登机口可以为空
                                assert(tasks.isNotEmpty()) {
                                    toastCenter("请选择任务")
                                    return@CustomButton
                                }
                                assert(nfcId.value.isNotEmpty()) {
                                    toastCenter("请输入NFC")
                                    return@CustomButton
                                }
                                //先根据nfc查询
                                viewModel.queryNfcBindType(
                                    nfcId.value, object : CallbackWithData<MixNfcTypeBO> {
                                        override fun success(data: MixNfcTypeBO) {
                                            super.success(data)
                                            var msg: String? = null

                                            data.user?.firstOrNull()?.let {
                                                //nfc绑定到人上，需要换绑到任务上
                                                msg =
                                                    "此NFC卡已经绑定到${it.truename},是否需要强制绑定到${
                                                        tasks.map { it.task_config.title }
                                                            .joinToString(",")
                                                    }?"

                                            } ?: data.car?.firstOrNull()?.let {
                                                //nfc绑定到车上，需要换绑到任务上
                                                msg =
                                                    "此NFC卡已经绑定到${it.title},是否需要强制绑定到${
                                                        tasks.map { it.task_config.title }
                                                            .joinToString(",")
                                                    }?"

                                            } ?: data.parking?.firstOrNull()?.let {
                                                //nfc已经绑定过任务了，是否需要换绑到其他任务上
                                                msg =
                                                    "此NFC卡已经绑定到其他任务,是否需要强制绑定到${
                                                        tasks.map { it.task_config.title }
                                                            .joinToString(",")
                                                    }?"
                                            } ?: run {
                                                //新的NFC卡,直接绑定
                                                msg = ""
                                            }

                                            msg?.let {
                                                forceUpdateTaskInfo(
                                                    viewModel = viewModel,
                                                    taskId = tasks.map { it.task_config.id },
                                                    nfcId = nfcId.value,
                                                    attachResourceType = positionType.value,
                                                    attachResourceName = when (positionType.value) {
                                                        AttachResourceType.GATE -> gate.value?.title
                                                            ?: ""

                                                        AttachResourceType.PARKING -> parking.value?.title
                                                            ?: ""

                                                        else -> ""
                                                    },
                                                    msg = it,
                                                    onSuccess = {
                                                        //tasks.clear()
                                                        nfcId.value = ""
                                                        gate.value = null
                                                        parking.value = null
                                                    }
                                                )
                                            }
                                        }
                                    })
                            }
                        }
                    }) {
                    Text(
                        text = "绑定",
                        fontSize = R.dimen.d14.dimenToSp(),
                        fontWeight = FontWeight.W400,
                        color = R.color.white.toComposeColor(),
                        modifier = Modifier.align(Alignment.Center)
                    )
                }
            }

            //复原参数
            LaunchedEffect(key1 = mCurrentSelectTab.intValue) {
                carNo.value = null
                person.value = null
                tasks.clear()
                parking.value = null
                gate.value = null
                nfcId.value = ""
            }
            //初始化一次
            LaunchedEffect(Unit) {
                //默认站点
                currentSite = iatas.firstOrNull() ?: ""
                //拉取保障单位
                viewModel.getUnitList()
            }
        }
        //悬浮按钮
        FloatDraggableButton(
            parentWidth = ScreenUtils.getAppScreenWidth(),
            parentHeight = ScreenUtils.getAppScreenHeight(),
            btnHeight = R.dimen.d40.getPxFromResource().toFloat(),
            btnWidth = R.dimen.d40.getPxFromResource().toFloat(),
            bgColor = statusBarColor.toComposeColor(),
            onClick = {
                showDetail(null)
            }
        ) {
            Text(
                text = "录入\n日志",
                fontSize = R.dimen.d12.dimenToSp(),
                fontWeight = FontWeight.W400,
                color = R.color.white.toComposeColor(),
                modifier = Modifier.align(Alignment.Center)
            )
        }
    }
}

//车辆录入入口
@Composable
private fun CarEntry(
    carNo: MutableState<NfcCarBO?>, nfcId: MutableState<String>
) {
    Column(
        modifier = Modifier.fillMaxWidth().padding(
            horizontal = R.dimen.d15.dimenToDp(), vertical = R.dimen.d12.dimenToDp()
        )
    ) {
        Card(
            modifier = Modifier.fillMaxWidth().intervalClick {
                showResourceList(
                    type = ResourceType.CAR,
                    listener = object : CallbackWithData<Any> {
                        override fun success(data: Any) {
                            super.success(data)
                            data as NfcCarBO
                            carNo.value = data
                        }
                    }
                )

            },
            elevation = CardDefaults.elevatedCardElevation(defaultElevation = R.dimen.d5.dimenToDp()),
            shape = RoundedCornerShape(R.dimen.d4.dimenToDp()),
            colors = CardDefaults.elevatedCardColors(containerColor = R.color.white.toComposeColor())
        ) {
            SingleSelectView(
                title = ResourceType.CAR.simple,
                content = carNo.value?.title ?: "",
                hint = ResourceType.CAR.hint
            )
        }
        VerticalSpace(R.dimen.d10.dimenToDp())
        Card(
            modifier = Modifier.fillMaxWidth(),
            elevation = CardDefaults.elevatedCardElevation(defaultElevation = R.dimen.d5.dimenToDp()),
            shape = RoundedCornerShape(R.dimen.d4.dimenToDp()),
            colors = CardDefaults.elevatedCardColors(containerColor = R.color.white.toComposeColor())
        ) {
            EditView(
                title = ResourceType.NFC.simple,
                content = nfcId.value,
                hint = ResourceType.NFC.hint,
                onChange = {
                    nfcId.value = it
                })
        }
    }
}

//人员录入入口
@Composable
private fun PersonEntry(
    person: MutableState<NfcUserBO?>, nfcId: MutableState<String>
) {
    Column(
        modifier = Modifier.fillMaxWidth().padding(
            horizontal = R.dimen.d15.dimenToDp(), vertical = R.dimen.d12.dimenToDp()
        )
    ) {
        Card(
            modifier = Modifier.fillMaxWidth().intervalClick {
                showResourceList(
                    type = ResourceType.PERSON,
                    listener = object : CallbackWithData<Any> {
                        override fun success(data: Any) {
                            super.success(data)
                            data as NfcUserBO
                            FyLog.e("人员", data.toJson())
                            person.value = data
                        }
                    }
                )
            },
            elevation = CardDefaults.elevatedCardElevation(defaultElevation = R.dimen.d5.dimenToDp()),
            shape = RoundedCornerShape(R.dimen.d4.dimenToDp()),
            colors = CardDefaults.elevatedCardColors(containerColor = R.color.white.toComposeColor())
        ) {
            SingleSelectView(
                title = ResourceType.PERSON.simple,
                content = person.value?.truename ?: "",
                hint = ResourceType.PERSON.hint
            )
        }
        VerticalSpace(R.dimen.d10.dimenToDp())
        Card(
            modifier = Modifier.fillMaxWidth(),
            elevation = CardDefaults.elevatedCardElevation(defaultElevation = R.dimen.d5.dimenToDp()),
            shape = RoundedCornerShape(R.dimen.d4.dimenToDp()),
            colors = CardDefaults.elevatedCardColors(containerColor = R.color.white.toComposeColor())
        ) {
            EditView(
                title = ResourceType.NFC.simple,
                content = nfcId.value,
                hint = ResourceType.NFC.hint,
                onChange = {
                    nfcId.value = it
                })
        }
    }
}


//任务录入入口
@Composable
private fun TaskEntry(
    tasks: SnapshotStateList<TaskItemBO>,
    parking: MutableState<ParkingItemBO?>,
    gate: MutableState<GateItemBO?>,
    nfcId: MutableState<String>,
    onSelectType: (AttachResourceType) -> Unit
) {

    //默认位置是机位类型
    val positionType = remember { mutableStateOf(AttachResourceType.PARKING) }
    Column(
        modifier = Modifier.fillMaxWidth().padding(
            horizontal = R.dimen.d15.dimenToDp(), vertical = R.dimen.d12.dimenToDp()
        )
    ) {
        Card(
            modifier = Modifier.fillMaxWidth().intervalClick {
                showResourceList(
                    type = ResourceType.TASK,
                    multiSelect = true,
                    data = tasks.map { it.task_config.id },
                    listener = object : CallbackWithData<Any> {
                        override fun success(data: Any) {
                            data as List<TaskItemBO>
                            super.success(data)
                            tasks.clear()
                            tasks.addAll(data)
                        }
                    }
                )
            },
            elevation = CardDefaults.elevatedCardElevation(defaultElevation = R.dimen.d5.dimenToDp()),
            shape = RoundedCornerShape(R.dimen.d4.dimenToDp()),
            colors = CardDefaults.elevatedCardColors(containerColor = R.color.white.toComposeColor())
        ) {
            MultiSelectView(
                title = ResourceType.TASK.simple,
                content = tasks.map { it.task_config.title },
                hint = ResourceType.TASK.hint,
                onDel = { index ->
                    tasks.removeAt(index)
                })
        }
        VerticalSpace(R.dimen.d10.dimenToDp())
        //位置
        Card(
            modifier = Modifier.fillMaxWidth().intervalClick {
                showResourceList(
                    type = positionType.value.linkResourceType,
                    listener = object : CallbackWithData<Any> {
                        override fun success(data: Any) {
                            super.success(data)
                            when (data) {
                                is ParkingItemBO -> {
                                    parking.value = data
                                    //其他的置空
                                    gate.value = null
                                }

                                is GateItemBO -> {
                                    gate.value = data
                                    //其他的置空
                                    parking.value = null
                                }

                                else -> {}
                            }
                        }
                    }
                )
            },
            elevation = CardDefaults.elevatedCardElevation(defaultElevation = R.dimen.d5.dimenToDp()),
            shape = RoundedCornerShape(R.dimen.d4.dimenToDp()),
            colors = CardDefaults.elevatedCardColors(containerColor = R.color.white.toComposeColor())
        ) {
            PositionSelectView(
                content = when (positionType.value) {
                    AttachResourceType.PARKING -> parking.value?.title ?: ""
                    AttachResourceType.GATE -> gate.value?.title ?: ""
                    else -> ""
                }, onSelectType = {
                    positionType.value = it
                    onSelectType(it)
                }, onDel = {
                    parking.value = null
                    gate.value = null
                })
        }

        VerticalSpace(R.dimen.d10.dimenToDp())
        //nfc
        Card(
            modifier = Modifier.fillMaxWidth(),
            elevation = CardDefaults.elevatedCardElevation(defaultElevation = R.dimen.d5.dimenToDp()),
            shape = RoundedCornerShape(R.dimen.d4.dimenToDp()),
            colors = CardDefaults.elevatedCardColors(containerColor = R.color.white.toComposeColor())
        ) {
            EditView(
                title = ResourceType.NFC.simple,
                content = nfcId.value,
                hint = ResourceType.NFC.hint,
                onChange = {
                    nfcId.value = it
                })
        }
    }
}


private fun Any.forceUpdateUerInfo(
    viewModel: RecorderCardViewModel,
    uid: String,
    nfcId: String,
    isForce: Boolean,
    msg: String,
    onSuccess: () -> Unit
) {
    //强制需要弹框提示一下
    if (isForce) {
        getConfirmDlg(
            "",
            msg,
            dismissOnBackPressed = false,
            dismissOnTouchOutside = false,
            confirmListener = object : SuccessCallback {
                override fun success() {
                    super.success()
                    viewModel.updateUserInfo(uid, nfcId, 1, 0, object : SuccessCallback {
                        override fun success() {
                            super.success()
                            toastCenter("人员绑定成功")
                            onSuccess()
                        }
                    })
                }
            })?.show()
    } else {
        viewModel.updateUserInfo(
            uid, nfcId, 0, 0, object : SuccessCallback {
                override fun success() {
                    toastCenter("人员绑定成功")
                    onSuccess()
                }
            }
        )
    }

}

private fun Any.forceUpdateCarInfo(
    viewModel: RecorderCardViewModel,
    carId: String,
    nfcId: String,
    msg: String,
    isForce: Boolean,
    onSuccess: () -> Unit
) {
    if (isForce) {
        getConfirmDlg(
            "",
            msg,
            dismissOnBackPressed = false,
            dismissOnTouchOutside = false,
            confirmListener = object : SuccessCallback {
                override fun success() {
                    super.success()
                    viewModel.updateCarInfo(
                        carId, nfcId, 1, 0, object : SuccessCallback {
                            override fun success() {
                                super.success()
                                toastCenter("车辆绑定成功")
                                onSuccess()
                            }
                        })
                }
            })?.show()
    } else {
        viewModel.updateCarInfo(
            carId, nfcId, 0, 0, listener = object : SuccessCallback {
                override fun success() {
                    toastCenter("车辆绑定成功")
                    onSuccess()
                }
            }
        )
    }

}


private fun Any.forceUpdateTaskInfo(
    viewModel: RecorderCardViewModel,
    taskId: List<String>,
    nfcId: String,
    attachResourceType: AttachResourceType,
    attachResourceName: String,
    msg: String,
    onSuccess: () -> Unit
) {
    if (msg.isNotEmpty()) {
        getConfirmDlg(
            "",
            msg,
            dismissOnBackPressed = false,
            dismissOnTouchOutside = false,
            confirmListener = object : SuccessCallback {
                override fun success() {
                    super.success()
                    viewModel.updateTaskInfo(
                        taskId, nfcId, attachResourceType, attachResourceName,
                        object : SuccessCallback {
                            override fun success() {
                                super.success()
                                toastCenter("任务绑定成功")
                                onSuccess()
                            }
                        })
                }
            }
        )?.show()
    } else {
        viewModel.updateTaskInfo(
            taskId, nfcId, attachResourceType, attachResourceName,
            object : SuccessCallback {
                override fun success() {
                    super.success()
                    toastCenter("任务绑定成功")
                    onSuccess()
                }
            })
    }
}

private fun showDetail(recorderType: RecorderType?) {
    ActivityUtils.getTopActivity()?.let {
        XPopup.Builder(it)
            .dismissOnBackPressed(true)
            .dismissOnTouchOutside(true)
            .asCustom(NfcLogPopView(it).apply {
            })
            .show()
    }
}

internal fun showResourceList(
    type: ResourceType?,
    listener: CallbackWithData<Any>,
    multiSelect: Boolean? = null,
    data: List<String>? = null,
    filterTask: List<String>? = null
) {
    ActivityUtils.getTopActivity()?.let {
        XPopup.Builder(it)
            .dismissOnBackPressed(true)
            .dismissOnTouchOutside(true)
            .asCustom(ResourceListPopView(it).apply {
                setType(type)
                setMultiSelect(multiSelect ?: false)
                setListener(listener)
                setData(data ?: emptyList())
                setFilterTask(filterTask ?: emptyList())
            }).show()
    }
}